import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class WebViewPage extends StatefulWidget {
  const WebViewPage({ super.key, required this.url });
  final String url;

  @override
  State<WebViewPage> createState() => _WebViewPage();
}

class _WebViewPage extends State<WebViewPage> {
  final _controller = WebViewController()
  ..setJavaScriptMode(JavaScriptMode.unrestricted);

  @override
  void initState() {
    super.initState();
    _controller.loadRequest(Uri.parse(widget.url));
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    
    return Scaffold(
      body: SafeArea(
        child: WebViewWidget(controller: _controller),
      ),
      floatingActionButton: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        spacing: 15,
        children: [
          IconButton(
            onPressed: () {
              _controller.reload();
            },
            icon: const Icon(Icons.refresh, color: Colors.amber, size: 30,),
          ),
          IconButton(
            onPressed: () async {
              final flag = await _controller.canGoBack();
              if (flag) {
                _controller.goBack();
              }
            },
            icon: const Icon(Icons.arrow_back_ios_new_rounded, color: Colors.pinkAccent, size: 30,),
          ),
          IconButton(
            onPressed: () async {
              final flag = await _controller.canGoForward();
              if (flag) {
                _controller.goForward();
              }
            },
            icon: const Icon(Icons.arrow_forward_ios_rounded, color: Colors.green, size: 30,),
          ),
        ],
      ),
    );
  }
}
